package chapter12

/**
  * @Copyright: Shanghai Definesys Company.All rights reserved.
  * @Description:
  * @author: chuhaitao
  * @since: 2019/1/18 22:53
  * @history:
  *          1.2019/1/18 created by chuhaitao
  */
object ObjectMachDemo {
  def main(args: Array[String]): Unit = {
    var num = 10001.0
    num match {
        //匹配Square(n)时，调用unapply（）方法
        //如果返回的是Some(math.sqrt(z))，则表示匹配成功
        //将值赋给n
      case Square(n) => println(n)
      case _=> println("null")
    }
  }
}

object Square {

  // unapply是对象提取器
  def unapply(z: Double): Option[Double] = Some(math.sqrt(z))

  def apply(z: Double): Double = z * z

}


